package com.example.xiaomiao.Controller;

import com.example.xiaomiao.DO.Note;
import com.example.xiaomiao.DTO.GetNoteRequestDTO;
import com.example.xiaomiao.DTO.NoteAddRequestDTO;
import com.example.xiaomiao.DTO.NoteDeleteRequestDTO;
import com.example.xiaomiao.DTO.Return;
import com.example.xiaomiao.Service.NoteService;
import com.example.xiaomiao.Util.JwtUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/note")
public class NoteController {
    @Resource
    NoteService noteService;

    @PostMapping("/add")
    public Return add(HttpServletRequest request, @RequestBody NoteAddRequestDTO noteAddRequestData){
        String token = JwtUtil.getTokenFromCookie(request);
        String id = JwtUtil.extractUsername(token);
        int userId = Integer.parseInt(id.substring(8));
        return noteService.add(userId,noteAddRequestData);
    }

    @PostMapping("delete")
    public Return delete(@RequestBody NoteDeleteRequestDTO noteDeleteRequestData){
        return noteService.delete(noteDeleteRequestData);
    }

    @PostMapping("update")
    public Return update(@RequestBody Note note){
        return noteService.update(note);
    }

    @PostMapping("get")
    public Return get(HttpServletRequest request, @RequestBody GetNoteRequestDTO getNoteRequestData){
        String token = JwtUtil.getTokenFromCookie(request);
        String id = JwtUtil.extractUsername(token);
        int userId = Integer.parseInt(id.substring(8));
        return noteService.get(userId,getNoteRequestData);
    }
}
